package com.myk.game.lib;

import android.os.Handler;

/**
 * 循环定时器
 */
public class LoopTimer {

    private int curTimes; //当前已经循环的次数
    private int loopTimes; //循环次数,若为负数则无限循环
    private long intervalMillis;    //间隔时间
    private boolean running;    //运行状态
    private Handler handler;    //消息处理器
    private ExecuteRunnable executeRunnable;    //执行Runnable

    /**
     * 创建一个循环定时器
     */
    public LoopTimer(Handler handler, Runnable runnable, long intervalMillis, int loopTimes) {
        this.handler = handler;
        this.intervalMillis = intervalMillis;
        this.loopTimes = loopTimes;
        this.curTimes = 0;
        executeRunnable = new ExecuteRunnable(runnable);
    }

    /**
     * 创建一个循环定时器
     */
    public LoopTimer(Runnable runnable, long intervalMillis, int loopTimes) {
        this(new Handler(), runnable, intervalMillis, loopTimes);
    }

    /**
     * 创建一个循环定时器，默认无限循环
     */
    public LoopTimer(Runnable runnable, long intervalMillis) {
        this(new Handler(), runnable, intervalMillis, -1);
    }

    /**
     * 创建一个循环定时器，默认间隔1秒
     */
    public LoopTimer(Runnable runnable) {
        this(runnable, 1000);
    }

    /**
     * 立即启动
     */
    public void start() {
        running = true;
        handler.removeCallbacks(executeRunnable);
        handler.post(executeRunnable);
    }

    /**
     * 延迟指定间隔毫秒启动
     */
    public void delayStart() {
        running = true;
        handler.removeCallbacks(executeRunnable);
        handler.postDelayed(executeRunnable, intervalMillis);
    }

    /**
     * 立即停止
     */
    public void stop() {
        running = false;
        handler.removeCallbacks(executeRunnable);
    }

    /**
     * 获取间隔毫秒
     */
    public long getIntervalMillis() {
        return intervalMillis;
    }

    /**
     * 设置间隔毫秒
     */
    public void setIntervalMillis(long intervalMillis) {
        this.intervalMillis = intervalMillis;
    }

    /**
     * 设置循环次数
     */
    public void setLoopTimes(int loopTimes) {
        this.loopTimes = loopTimes;
    }

    /**
     * 获取循环次数
     */
    public int getLoopTimes() {
        return loopTimes;
    }

    /**
     * 是否正在运行
     */
    public boolean isRunning() {
        return running;
    }

    /**
     * 设置消息处理器
     */
    public void setHandler(Handler handler) {
        this.handler = handler;
    }

    /**
     * 设置执行内容
     */
    public void setRunnable(Runnable runnable) {
        executeRunnable.setRunnable(runnable);
    }

    /**
     * 执行Runnable
     */
    private class ExecuteRunnable implements Runnable {
        private Runnable runnable;

        public ExecuteRunnable(Runnable runnable) {
            this.runnable = runnable;
        }

        @Override
        public void run() {
            if (running && runnable != null) {
                curTimes++;
                runnable.run();
                if (loopTimes > 0 && curTimes >= loopTimes) {
                    running = false;
                }
                else {
                    handler.postDelayed(executeRunnable, intervalMillis);
                }
            }
        }

        public void setRunnable(Runnable runnable) {
            this.runnable = runnable;
        }
    }
}